﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.Caching;
using System.Reflection;
using System.Threading;
using System.Net;
using System.IO;
using System.Collections;
using System.Web;
using System.Text.RegularExpressions;
using log4net;
namespace SpiderService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        BLL.AutoCheckMain main;
        private void button4_Click(object sender, EventArgs e)
        {
            //BLL.Message.AsyncSendEmail.Send("yuebing@koubei.com", "test", "test", null);
            //return;
            // MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString() + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            BLL.CheckMain main = new BLL.AutoCheckMain();
            main.xMain();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
          ///  return;
            if (main == null)
                main = new BLL.AutoCheckMain();
            main.xMain();
            this.timer1.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BLL.Message.MyLog4Net.Write("timer", "webcheck timer");
            try
            {

                timer1.Enabled = false;
                BLL.CheckMain main = new BLL.AutoCheckMain();
                main.xMain();
                this.timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                BLL.Message.MyLog4Net.Write(ex.Message + ex.StackTrace, "timer");
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string encodingName = string.Empty;
            WebRequest webRequest = WebRequest.Create("http://topic.csdn.net/t/20020406/10/626789.html");
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            byte[] htmlByte = GetByteContent(stream);
            stream.Close();
            string temp = Encoding.GetEncoding("utf-8").GetString(htmlByte);
            string reg_charset = "(]*charset=(?[^>'\"]*)[\\s\\S]*?>)|(xml[^>]+encoding=(\"|')*(?[^>'\"]*)[\\s\\S]*?>)";
            Regex r = new Regex(reg_charset, RegexOptions.IgnoreCase);
            Match m = r.Match(temp);
            encodingName = (m.Captures.Count != 0) ? m.Result("${charset}") : "";

            string html = Encoding.GetEncoding(encodingName).GetString(htmlByte);
        }
 //GetByteContent函数

        private byte[] GetByteContent(Stream stream)
        {
            ArrayList arBuffer = new ArrayList();

            byte[] buffer = new     byte[1024];
            int offset = 1024;
            int count = stream.Read(buffer, 0, offset);
            while (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    arBuffer.Add(buffer);
                }
                count = stream.Read(buffer, 0, offset);
            }

            return (byte[])arBuffer.ToArray(System.Type.GetType("System.Byte"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                System.IO.TextReader rd = new System.IO.StreamReader(@"d:\19321be6-66ab-4a8c-b97d-7a686042e404.html");
                string html = rd.ReadToEnd();


                MatchCollection matchs = Regex.Matches(html, "分钟之前", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            }
        }

    }

       

}
